package com.mmdccj.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mmdccj.reggie.common.R;
import com.mmdccj.reggie.entity.Category;
import com.mmdccj.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("category:{}",category);
        categoryService.save(category);
        return R.success("新增分类成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        Page pageInfo = new Page<Category>(page,pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Category::getSort);
        categoryService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /***
     * 更新套餐
     * @param category 套餐更新数据包含id
     * @return 更新结果
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("更新成功");
    }

    /***
     * 删除套餐
     * @param ids 套餐id
     * @return 删除结果
     */
    @DeleteMapping
    public R<String> delete(long ids){
        log.info("待删除id为{}",ids);
        categoryService.remove(ids);
        return R.success("成功删除");
    }

    /***
     * 添加菜品页面中的获取菜品/套餐分类
     * @param category
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        LambdaQueryWrapper<Category> query = new LambdaQueryWrapper<>();
        query.eq(category.getType() != null,Category::getType,category.getType());
        query.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list();
        return R.success(list);
    }

}



